package com.tianyang.yukuaifu.module.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.module.dialog.interfaces.IDateValidListener;
import com.tianyang.yukuaifu.module.roundview.RoundTextView;
import com.tianyang.yukuaifu.utils.ToastUtil;

/**
 * Description：日期有效期选择
 * Created by 薛勇军 on 2023/10/20.
 */
public class DateValidDialog {

    private RelativeLayout longTermRl, notLongTermRl;

    private ImageView longTermIv, notLongTermIv;

    private RoundTextView cancelRtv, confirmRtv;

    private IDateValidListener listener;

    private int chooseIndex = -1;

    private Dialog aDialog;

    public void setListener(IDateValidListener listener) {
        this.listener = listener;
    }

    public DateValidDialog(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_date_valid, null);
        aDialog = new Dialog(context, R.style.bottom_pop_menu);
        longTermRl = inflate.findViewById(R.id.longTermRl);
        notLongTermRl = inflate.findViewById(R.id.notLongTermRl);
        longTermIv = inflate.findViewById(R.id.longTermIv);
        notLongTermIv = inflate.findViewById(R.id.notLongTermIv);
        cancelRtv = inflate.findViewById(R.id.cancelRtv);
        confirmRtv = inflate.findViewById(R.id.confirmRtv);

        setUiBeforShow(context);

        aDialog.setContentView(inflate); // 把自定義view加上去
//        aDialog.setCanceledOnTouchOutside(false);
//        aDialog.setCancelable(false);
    }

    public void setUiBeforShow(final Context context) {
        cancelRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        longTermRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseIndex = 0;
                longTermIv.setImageResource(R.drawable.ic_choose_checked);
                notLongTermIv.setImageResource(R.drawable.ic_choose_normal);
            }
        });
        notLongTermRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseIndex = 1;
                longTermIv.setImageResource(R.drawable.ic_choose_normal);
                notLongTermIv.setImageResource(R.drawable.ic_choose_checked);
            }
        });
        confirmRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chooseIndex < 0) {
                    ToastUtil.show("请选择身份证有效期");
                    return;
                }
                if (listener != null) {
                    listener.onDateValid(chooseIndex == 0);
                }
                dismiss();
            }
        });
    }

    public void dismiss() {
        if (aDialog != null && aDialog.isShowing()) {
            aDialog.dismiss();
        }
    }

    public void show() {
        if (aDialog != null && !aDialog.isShowing()) {
            aDialog.show();
        }
    }
}
